package com.shinowit.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shinowit.dao.mapper.MerchandiseInfoMapper;
import com.shinowit.model.DataGridResult;
import com.shinowit.model.MerchandiseInfo;
import com.shinowit.model.MerchandiseInfoCriteria;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

@Controller
@RequestMapping(value="/stock_info")
public class StockInfoViewController {

	@Resource
	private MerchandiseInfoMapper product_info_dao;
	
	@Resource
	private JdbcTemplate jt;
	
	@RequestMapping(value={"","/"},method=RequestMethod.GET)
	public String defaultPage(){
		return "/stock_info/list";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public DataGridResult<MerchandiseInfo> getPage(@RequestParam(value="page",defaultValue="1") Integer pageIndex,@RequestParam(value="rows",defaultValue="10") Integer pageSize,MerchandiseInfo product_info,String sort,String order){
		DataGridResult<MerchandiseInfo> result=new DataGridResult<MerchandiseInfo>();
		
		MerchandiseInfoCriteria ex=new MerchandiseInfoCriteria();
		MerchandiseInfoCriteria.Criteria cr1= ex.createCriteria();
		
		if (null!=product_info){
			if ((product_info.getMerchandiseName()!=null) && (product_info.getMerchandiseName().trim().length()>0)){
				cr1.andMerchandiseNameLike(product_info.getMerchandiseName()+"%");
			}
			if ((product_info.getMerchandiseAb()!=null) && (product_info.getMerchandiseAb().trim().length()>0)){
				cr1.andMerchandiseAbLike(product_info.getMerchandiseAb()+"%");
			}
			if ((product_info.getTypeCode()!=null) && (product_info.getTypeCode().trim().length()>0)){
				cr1.andTypeCodeEqualTo(product_info.getTypeCode());
			}
		}
		
		ex.setPageIndex(pageIndex);
		ex.setPageSize(pageSize);
		
		if ((null!=sort) && (sort.trim().length()>0)){
			ex.setOrderByClause(sort);
		}
		
		if ((null!=order) && (order.trim().length()>0)){
			if (!"asc".equalsIgnoreCase(order)){
				ex.setOrderByClause(ex.getOrderByClause()+" desc");
			}
		}
		
		List<MerchandiseInfo> data_list=product_info_dao.selectStockInfoByExamplePage(ex);
		result.setRows(data_list);
		
		long record_count=product_info_dao.countByExample(ex);
		result.setTotal(record_count);
		
		return result;
	}		
	
	
	@RequestMapping(value="/type_list")
	@ResponseBody
	public  List<Map<String,Object>> get_type_list(){
		return jt.queryForList("select type_code as id,type_name as text from merchandise_type_info");
	}
	
	@RequestMapping(value = "/report", method = RequestMethod.GET)  
	public String report(Model model,@RequestParam(value="format",defaultValue="xls") String format) {
		MerchandiseInfoCriteria ex=new MerchandiseInfoCriteria();
		ex.setPageSize(Integer.MAX_VALUE);
		ex.setPageIndex(1);
		
		List<MerchandiseInfo> list_data=product_info_dao.selectStockInfoByExamplePage(ex);
	    // 报表数据源  
	    JRDataSource jrDataSource = new JRBeanCollectionDataSource(list_data,false);  
	          
	    // 动态指定报表模板url  
	    model.addAttribute("url", "/WEB-INF/jasper/stock_info.jasper");  
	    model.addAttribute("format", format); // 报表格式 ,xls,pdf,html等  
	    model.addAttribute("jrMainDataSource", jrDataSource);  
	          
	    return "iReportView"; // 对应jasper-defs.xml中的bean id  
	}  	
}
